# set tmux position
set-option -g status-position bottom

# improve colors
# set -g default-terminal "tmux-256color"
# set -g default-terminal "${TERM}"
set -ga terminal-overrides ",*256col*:Tc"

# act like vim
setw -g mode-keys vi

is_vim="ps -o state= -o comm= -t '#{pane_tty}' | grep -iqE '^[^TXZ ]+ +(\\S+\\/)?g?\.?(view|n?vim?x?)(-wrapped)?(diff)?$'"

bind-key -n 'C-h' if-shell "$is_vim" 'send-keys C-h' 'select-pane -L'
bind-key -n 'C-j' if-shell "$is_vim" 'send-keys C-j' 'select-pane -D'
bind-key -n 'C-k' if-shell "$is_vim" 'send-keys C-k' 'select-pane -U'
bind-key -n 'C-l' if-shell "$is_vim" 'send-keys C-l' 'select-pane -R'

bind-key -T copy-mode-vi 'C-h' select-pane -L
bind-key -T copy-mode-vi 'C-j' select-pane -D
bind-key -T copy-mode-vi 'C-k' select-pane -U
bind-key -T copy-mode-vi 'C-l' select-pane -R

bind h select-pane -L
bind j select-pane -D
bind k select-pane -U
bind l select-pane -R


# rename pane
bind-key / command-prompt -p "Rename pane:" "select-pane -T %1"
set -g pane-border-status top
set -g pane-border-format " [ ###P #T #{pane_current_path} ] "
# set -g pane-border-format " [ ###P #{pane_current_path} ] "


# rename session
bind-key \ command-prompt "rename-session '%%'"

# move window
bind-key p swap-window -t -1
bind-key n swap-window -t +1
bind-key -n C-S-Left previous-window
bind-key -n C-S-Right next-window

# jump to the last window
bind-key . last-window

# Below two lines have conficts with panne selction
# bind -r C-h select-window -t :-
# bind -r C-l select-window -t :+

# server use: disable ctrl jkhl to select pane
# bind-key -n C-h  "send-keys C-h"
# bind-key -n C-j  "send-keys C-j"
# bind-key -n C-k  "send-keys C-k"
# bind-key -n C-l  "send-keys C-l"

bind-key m copy-mode    # enter copy mode
bind v paste-buffer     # paste content
bind -T copy-mode-vi "v" send -X begin-selection
bind -T copy-mode-vi "V" send -X rectangle-toggle
bind -T copy-mode-vi "y" send -X copy-selection-and-cancel
# bind -T copy-mode-vi y send-keys -X copy-pipe-and-cancel 'xclip -in -selection clipboard'
# bind-key -T copy-mode-vi y send-keys -X copy-pipe-and-cancel "netcat -c localhost 8377"
# bind-key -T copy-mode-vi y send-keys -X copy-pipe-and-cancel "netcat -c 127.0.0.1 8377"


# act like GNU screen
unbind C-b
set -g prefix C-a
# bind a send-prefix
# bind C-a send-prefix
# bind b send-prefix
# bind C-b send-prefix

# create new windows/panes in same directory
bind c new-window -c "#{pane_current_path}"
bind-key | split-window -h
bind-key - split-window -v

# quit window
unbind x
bind-key X kill-pane
# bind-key Q kill-window
# bind K confirm kill-window

# enable and disable keyboard input for panes
bind < select-pane -d \; display-message "input disabled"
bind > select-pane -e \; display-message "input enabled"

# size windows based on clients looking at that window
setw -g aggressive-resize on

# enable mouse control
set -g mouse on

# start window numbers at 1 to match keyboard order with tmux order
set -g base-index 1

# start pane numbers at 1 to match keyboard order with tmux order
setw -g pane-base-index 1

# easy resizing of panes
bind -r J resize-pane -D 5
bind -r K resize-pane -U 5
bind -r H resize-pane -L 5
bind -r L resize-pane -R 5

# bind-key -n C-Left  "resize-pane -L 5"
# bind-key -n C-Right "resize-pane -R 5"
# bind-key -n C-Up    "resize-pane -U 5"
# bind-key -n C-Down  "resize-pane -D 5"
# 

# reenable automatic renaming for the current window
bind N setw automatic-rename on \; display-message "automatic rename enabled"

# renumber windows sequentially after closing any of them
set -g renumber-windows on

# monitor windows for activity
setw -g monitor-activity on

# don't wait for escape sequences
set -sg escape-time 0

# display pane numbers for longer
set -g display-panes-time 2000

# NOTE: belowe settings are overridden by github_* theme
set -g status-left-length 20
set -g status-left '#{?pane_input_off,#[fg=colour160],#[fg=colour136]}[#[fg=colour136]#S:#I.#P#{?pane_input_off,#[fg=colour160],#[fg=colour136]}] '
set -g status-right '#[fg=colour166]#([ "$(tmux show-option -g prefix)" != "prefix C-a" ] && echo "($(tmux show-option -g prefix | cut -c 8-)) ")#[fg=colour33]%d %b %Y #{?client_prefix,#[fg=colour160],#[fg=colour61]}- #[fg=colour64]%I:%M %p #{?client_prefix,#[fg=colour160],#[fg=colour61]}- #[fg=colour37]#([ -f $HOME/.name ] && cat $HOME/.name || hostname)'
# set -g status-right '#[fg=colour166]#U@#H #{?client_prefix,#[fg=colour160],#[fg=colour61]}- #[fg=colour33]%d %b #{?client_prefix,#[fg=colour160],#[fg=colour61]}- #[fg=colour64]%I:%M %p #{?client_prefix,#[fg=colour160],#[fg=colour61]}'

# increase scrollback lines
set -g history-limit 65536

# keybinding to clear history
bind C-k clear-history \; display-message "history cleared"

# C-b ! breaks current pane into separate window
# join a pane/window from the current session into the current window
bind @ command-prompt -p "create pane from:" "join-pane -s ':%%'"

# reload the .tmux.conf file with Ctrl-r
bind C-r source-file ~/.tmux.conf \; display-message "configuration reloaded"

# toggle passthrough of prefix
bind P if-shell "[ \"$(tmux show-options -g prefix)\" = \"prefix C-a\" ]" '\
    set -g prefix C-b; display-message "passthrough enabled"; refresh-client -S; \
    ' '\
    set -g prefix C-a; display-message "passthrough disabled"; refresh-client -S; \
    '

#### color (Solarized 256)
set -g mode-style "fg=default,bg=default,reverse"
set -g status-style bg=colour235,fg=colour136
setw -g window-status-style fg=colour244
setw -g window-status-current-style fg=colour166
setw -g window-status-activity-style fg=colour61
setw -g window-status-bell-style fg=colour61
set -g pane-border-style fg=colour240
set -g pane-active-border-style fg=colour255
set -g message-style bg=colour235,fg=colour166

# pane number display
set -g display-panes-active-colour colour166 #blue
set -g display-panes-colour colour33 #orange

# clock
setw -g clock-mode-colour colour64 #green

# Update config
bind-key r source-file ~/.tmux.conf \; display-message "tmux.conf reloaded"

# Allow local customization in ~/.tmux_local.conf
if-shell "[ -f ~/.tmux_local.conf ]" 'source ~/.tmux_local.conf'

# Undercurl
set -as terminal-overrides ',*:Smulx=\E[4::%p1%dm'  # undercurl support
set -as terminal-overrides ',*:Setulc=\E[58::2::%p1%{65536}%/%d::%p1%{256}%/%{255}%&%d::%p1%{255}%&%d%;m'  # underscore colours - needs tmux-3.0

# plugins
source "$HOME/.config/tmux/plugins.conf"
# theme
# source "~/.config/tmux/themes/github_dark.conf"

